package com.example.pmxt.modules.Notice;

import com.example.pmxt.common.ReturnResult;
import com.example.pmxt.domain.Carousel;
import com.example.pmxt.domain.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@RestController
@Validated
@RequestMapping("/notices")
public class NoticeController {

    @Value("${file.location}")
    String filelocation;
    @Value("${file.path}")
    String filepath;

    private final NoticeService noticeService;

    public NoticeController(@Autowired NoticeService noticeService){
        this.noticeService = noticeService;
    }

    //新增拍卖会
    @PostMapping("/addNotice")
    public ReturnResult addNotice(MultipartFile file, @Validated Notice notice) {
        if (file.isEmpty()) {
            return ReturnResult.buildFailureResult("文件为空");
        } else {
            try {
                // 保存文件
                // 服务器文件名
                String serverFilename = UUID.randomUUID().toString().substring(0, 8) + ".jpg";
                // 创建目录、存储图片地址目录
                String path = filelocation;
                System.out.println(path);
                // 存储图片的url
                Files.createDirectories(Paths.get(path));
                // 保存
                file.transferTo(Path.of(path, serverFilename));

                // 记录上传图片url
                notice.setScfj("download/"  + serverFilename);


                return ReturnResult.buildSuccessResult(noticeService.save(notice));
//                return ReturnResult.buildSuccessResult("导入成功", true);
            } catch (Exception e) {
                e.printStackTrace();
                return ReturnResult.buildFailureResult(e.toString());
            }
        }
    }

    //逻辑删除拍卖会
    @GetMapping("/deleteNoticeById")
    public ReturnResult deleteNoticelById( @Validated  Integer id) {
        return ReturnResult.buildSuccessResult(noticeService.delete(id));
    }

    //查询全部
    @GetMapping("/getNoticeAll")
    public ReturnResult getNoticeAll(){
        return ReturnResult.buildSuccessResult(noticeService.list());
    }

    //修改拍卖会
    @PutMapping("/updateNoticeById")
    public ReturnResult updateNoticeById(MultipartFile file, @Validated Notice notice ) {
        if (file.isEmpty()) {
            return ReturnResult.buildFailureResult("图片为空");
        } else {
            try {
                // 保存图片
                // 服务器文件名
                String serverFilename = UUID.randomUUID().toString().substring(0, 8) + ".jpg";
                // 创建目录
                String path = filelocation;
                System.out.println(path);
                Files.createDirectories(Paths.get(path));
                // 保存
                file.transferTo(Path.of(path, serverFilename));

                // 记录上传图片url
                notice.setScfj("download/"  + serverFilename);

                return ReturnResult.buildSuccessResult(noticeService.updateById(notice));
            } catch (Exception e) {
                e.printStackTrace();
                return ReturnResult.buildFailureResult(e.toString());
            }
        }
    }
}
